package com.aurora.source.widget;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aurora.source.R;
import com.aurora.source.base.WebViewActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author: Lance
 * @description: 隐私政策 弹窗
 * @date: 2018/11/5 21:39
 * @version: 1.0
 */
public class PrivacyPolicyPopupWindow extends PopupWindow {
    View arcView;
    @BindView(R.id.tv_privacy_policy_no)
    TextView tvPrivacyPolicyNo;
    @BindView(R.id.tv_privacy_policy_sure)
    TextView tvPrivacyPolicySure;
    @BindView(R.id.tv_first)
    TextView tvFirst;
    @BindView(R.id.tv_second)
    TextView tvSecond;

    @BindView(R.id.rl_privacy_policy)
    RelativeLayout rlPrivacyPolicy;

    private View mMenuView;

    public boolean isShow;

    public PrivacyPolicyPopupWindow(final Context context, View.OnClickListener listener, View view) {
        arcView = view;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.layout_privacy_policy_popupwindow, null);
        ButterKnife.bind(this, mMenuView);

        tvPrivacyPolicyNo.setOnClickListener(listener);
        tvPrivacyPolicySure.setOnClickListener(listener);

        SpannableString ssFirst = new SpannableString(tvFirst.getText().toString());
        ssFirst.setSpan(new UnderlineSpan(), 18, 24, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ssFirst.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, WebViewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("title","区块链溯源系统隐私政策");
                bundle.putString("url","https://iic.api.aoruola.net.cn/iic-api-front/h5/article/articleDetail.html?classCode=setting_protocol_declare&title=区块链溯源系统隐私政策");
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        }, 18, 24, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ssFirst.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)), 18, 24, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvFirst.setText(ssFirst);
        tvFirst.setHighlightColor(ContextCompat.getColor(context, android.R.color.transparent));
        //  为了响应点击
        tvFirst.setMovementMethod(LinkMovementMethod.getInstance());

        SpannableString ssSecond = new SpannableString(tvSecond.getText().toString());
        ssSecond.setSpan(new UnderlineSpan(), 18, 24, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ssSecond.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, WebViewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("title","区块链溯源系统隐私政策");
                bundle.putString("url","https://iic.api.aoruola.net.cn/iic-api-front/h5/article/articleDetail.html?classCode=setting_protocol_declare&title=区块链溯源系统隐私政策");
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        }, 18, 24, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        ssSecond.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)), 18, 24, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvSecond.setText(ssSecond);
        tvSecond.setHighlightColor(ContextCompat.getColor(context,android.R.color.transparent));
        //  为了响应点击
        tvSecond.setMovementMethod(LinkMovementMethod.getInstance());


        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);

        rlPrivacyPolicy.getBackground().setAlpha(100);

        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (isShow) {
                    showAsDropDown(arcView);
                }
            }
        });
    }
}
